using System;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using ipi.MCC.Global;

namespace ipi.MCC.Controls
{

    public partial class MCCTreeControl
	{
        protected bool bCheckable;
        
        private StringCollection CheckedNodeList
        {
            get
            {
                StringCollection scCheckedNode;

                if (ViewState[scCheckedNodeList] != null)
                {
                    scCheckedNode = (StringCollection)ViewState[scCheckedNodeList];
                }
                else
                {
                    scCheckedNode = new StringCollection();
                }

                return scCheckedNode;
            }

            set
            {
                if (ViewState[scCheckedNodeList] != null)
                {
                    ViewState[scCheckedNodeList] = value;
                }
                else
                {
                    ViewState.Add(scCheckedNodeList, value);
                }
            }
        }

        public StringCollection CheckedNodes()
        {
            StringCollection scResult = new StringCollection();
            StringCollection scOriginalCheckedNodes = CheckedNodeList;

            foreach (TreeNode tnChecked in trvLawDoc.CheckedNodes)
            {
                string sNodeID = tnChecked.Value;
                scResult.Add(sNodeID);

                if (scOriginalCheckedNodes.Contains(sNodeID))
                {
                    scOriginalCheckedNodes.Remove(sNodeID);
                }
            }

            foreach (string sLeftNodeIDs in scOriginalCheckedNodes)
            {
                TreeNode tnExits = null;
                this.GetNodeByValue(Convert.ToInt32(sLeftNodeIDs), trvLawDoc.Nodes, ref tnExits);
                if (tnExits == null)
                {
                    scResult.Add(sLeftNodeIDs);
                }
            }
            return scResult;
        }

        public void SetToCheckNodeID(int iNodeID)
        {
            StringCollection scCheckedNode = CheckedNodeList;
            scCheckedNode.Add(iNodeID.ToString());
            CheckedNodeList = scCheckedNode;
        }

        public bool Checkable
        {
            get
            {
                return bCheckable;
            }
            set
            {
                bCheckable = value;
            }
        }

        public bool NodeChecked()
        {
            if (this.CheckedNodeList.Count > 0)
            {
                return true;
            }
            else
            {
                validGlobal.ErrorMessage = Exceptions.HandleException(new UserHandlingException(Resources.Resources.MCCTreeControlType_NodeSelected), this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Exception);
                validGlobal.IsValid = false;
                return false;
            }
        }

        private void CheckNode(SPList splNodes, StringCollection ListNodeID)
        {
            //			CheckNode(ListNodeID, trvLawDoc.Nodes);
            DataTable dtTree = GetTreeTable(splNodes);

            foreach (string sCheckedNodeID in ListNodeID)
            {
                TreeNode tnCurrentChecked = null;
                ExpandToNode(Convert.ToInt32(sCheckedNodeID), dtTree, ref tnCurrentChecked);
                if (tnCurrentChecked != null)
                {
                    tnCurrentChecked.Checked = true;
                }
            }
        }

	}
}